package com.crazy.web.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crazy.enums.Constants;
import com.crazy.model.major.dto.UserRoleDto;
import com.crazy.model.major.dto.UserSession;
import com.crazy.model.major.entity.TUser;
import com.crazy.model.major.entity.TUserRole;
import com.crazy.model.major.service.TUserService;
import com.crazy.rest.RestResult;
import com.crazy.web.controller.BaseController;
import com.crazy.web.service.share.UserReviewService;
import com.crazy.web.service.user.RoleService;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户审核接口
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/12/14 9:22
 */
@Controller
@Log4j2
@RequestMapping("/user/review")
public class UserReviewController extends BaseController {

    @Autowired
    private UserReviewService userReviewService;
    @Autowired
    private RoleService roleService;

    /**
     * 打开用户审核页面
     * @return
     */
    @RequiresPermissions("review:view")
    @GetMapping("/view")
    public String view() {
        request.setAttribute("url", "/user/review/view");
        return "review/view";
    }

    /**
     * 查询用户列表
     * @param page
     * @param rows
     * @param search
     * @return
     */
    @RequiresPermissions("review:select")
    @GetMapping("/select")
    @ResponseBody
    public RestResult select(@RequestParam int page, @RequestParam int rows, @RequestParam(required = false) String search) {
        UserSession userSession = getUserSession();
        IPage<TUser> userIPage = userReviewService.find(userSession.getId(), page, rows, search);
        return RestResult.createSuccessResult("查询成功", userIPage);
    }

    @RequiresPermissions("review:lock")
    @GetMapping("/lock")
    @ResponseBody
    public RestResult lock(@RequestParam Long id) {
        userReviewService.lock(id, 1);
        return RestResult.createSuccessResult("上锁成功");
    }

    @RequiresPermissions("review:lock")
    @GetMapping("/unlock")
    @ResponseBody
    public RestResult unlock(@RequestParam Long id) {
        userReviewService.lock(id, 0);
        return RestResult.createSuccessResult("解锁成功");
    }

    @GetMapping("/allRole")
    @ResponseBody
    public RestResult allRole(@RequestParam Long userId) {
        List<UserRoleDto> list = roleService.all(userId);
        return RestResult.createSuccessResult("查询成功", list);
    }

    /**
     * roles: roles,
     *                 userId: userId
     * @return
     */
    @PostMapping("/tiedRole")
    @ResponseBody
    public RestResult tiedRole(@RequestParam Long userId, @RequestParam(value="roles[]", required = false) Long[] roles) {
        roleService.tieRole(userId, roles);
        return RestResult.createSuccessResult("绑定成功");
    }
}
